<?
die("Stopp!");

include("bildskalning.inc.php");
include("conf.inc.php");
include("functions.php");

set_time_limit(0);

dbConnect();

// Tömmer tblMedlemmar
$sSql = "TRUNCATE TABLE tblMedlemmar";
mysql_query($sSql) or die(mysql_error());

$arrRegioner = array("Blekinge" => 1, "Borås" => 2, "Eskilstuna" => 3, "Falun" => 4, "Gotland" => 5, "Gävle" => 6, "Göteborg" => 7, 
	"Halmstad" => 8, "Helsingborg" => 9, "Jönköping" => 10, "Kalmar" => 11, "Karlstad" => 12, "Kristianstad" => 13, "Luleå" => 14, 
	"Malmö" => 15, "Mariestad" => 16, "Norrköping/Linköping" => 17, "Skellefteå" => 18, "Skövde" => 19, "Stockholm" => 20, 
	"Sundsvall" => 21, "Uddevalla" => 22, "Umeå" => 23, "Uppsala" => 24, "Västerås" => 25, "Växjö" => 26, "Ystad" => 27, 
	"Örebro" => 28, "Östersund" => 29);
	
$arrProps = array("Bollar" => 1, "Käglor" => 2, "Ringar" => 3, "Poi" => 4, "Diabolo" => 5, "Devilstick" => 6, "Lindans" => 7, 
	"Rola-Bola" => 8, "Pilla naveln" => 9, "Tallrik" => 10);

for($memberId = 1; $memberId <= 3200; $memberId++) {
	if($source = @file_get_contents("http://alive.jongleringsoasen.se/userinfo.asp?UserID=" . $memberId)) {
	
		if(preg_match("/<h3>Medlemsinfo om (.*?)<\/h3>/", $source, $result)) {
			$username = $result[1];
		} else {
			$username = "";
		}
		
		if(preg_match('/<td align="left"><font size="1">Namn <\/font><\/td>(.*?)<td><font size="1">(.*?)<\/font><\/td>/s', $source, $result)) {
			$namn = strip_tags($result[2]);
		} else {
			$namn = "";
		}
		
		if(preg_match('/<td align="left"><font size="1">F(.*?)r<\/font><\/td>(.*?)<td 1">(.*?)<\/font><\/td>/s', $source, $result)) {
			$year = $result[3];
		} else {
			$year = "";
		}
		
		$regionsId = 0;
		$ort = "";
		
		if(preg_match('/<td align="left"><font size="1">Bor i<\/font><\/td>(.*?)<td><font size="1">(.*?),(.*?)<\/font><\/td>/s', $source, $result)) {
			if(isset($arrRegioner[utf8_encode($result[2])])) {
				$regionsId = $arrRegioner[utf8_encode($result[2])];
			}
			
			$ort = trim(strip_tags($result[3]));
		}
		
		$propId = 0;
		
		if(preg_match('/<td align="left"><font size="1">Pysslar helst med<\/font><\/td>(.*?)<td><font size="1">(.*?)<\/font><\/td>/s', $source, $result)) {
			$propName = utf8_encode(trim(strip_tags($result[2])));
			
			if(isset($arrProps[$propName])) {
				$propId = $arrProps[$propName];
			}
		}
		
		if(preg_match('/<td align="left"><font size="1">ICQ nummer<\/font><\/td>(.*?)<td><font size="1">(.*?)<\/font><\/td>/s', $source, $result)) {
			$icq = $result[2];
		} else {
			$icq = "";
		}
		
		if(preg_match('/<td align="left"><font size="1">Statuspo(.*?)ng<\/font><\/td>(.*?)<td><font size="1">(.*?)<\/font><\/td>/s', $source, $result)) {
			$status = $result[3];
		} else {
			$status = "";
		}
		
		if(preg_match('/<font size="1"><a href="userinfo\.asp\?userID=([0-9]+)">(.*?)<\/a><\/font>/s', $source, $result)) {
			$varvadAvId = $result[1];
		} else {
			$varvadAvId = "";
		}
		
		if(preg_match('/<td align="left"><font size="1">Senast inloggad<\/font><\/td>(.*?)<td><font size="1">(.*?)<\/font><\/td>/s', $source, $result)) {
			$inloggad = $result[2];
		} else {
			$inloggad = "";
		}
		
		if(preg_match('/<td align="left"><font size="1">Antal inloggningar<\/font><\/td>(.*?)<td><font size="1">([0-9]+)<\/font><\/td>/s', $source, $result)) {
			$inloggningar = $result[2];
		} else {
			$inloggningar = "";
		}
		
		if(preg_match('/<\!-- <div style="border: 1px solid #FF00FF; overflow:auto; width:300%"> -->(.*?)&nbsp;(.*?)<\!--/s', $source, $result)) {
			$beskrivning = $result[1];
			$beskrivning = preg_replace("/<style(.*?)\/style>/s", "", $beskrivning);	
			$beskrivning = trim(strip_tags($beskrivning));
		} else {
			$beskrivning = "";
		}
		
		$imgName = "";
		
		if(preg_match('/"\/images\/users\/(.*?)"/s', $source, $result)) {
			if($result[1] != "default.jpg") {
				$imgPath = "http://alive.jongleringsoasen.se/images/users/" . $result[1];
				
				$im = imagecreatefromjpeg($imgPath);
				
				$imgName = strtoupper(substr(md5(microtime(true) * rand(0, 4000)), 0, 20)) . ".jpg";
				
				imagejpeg($im, "bilder/medlemmar/150px/" . $imgName, 100);
				
				imagedestroy($im);
				
				cropImage("bilder/medlemmar/150px/" . $imgName, "bilder/medlemmar/50x50/" . $imgName, 50, 50);
			}
		}
		
		$sSql = "INSERT INTO tblMedlemmar SET
				id = '" . mysql_real_escape_string($memberId) . "',
				username = '" . mysql_real_escape_string(utf8_encode($username)) . "',
				name = '" . mysql_real_escape_string(utf8_encode($namn)) . "',
				year = '" . mysql_real_escape_string($year) . "',
				regionsId = '" . mysql_real_escape_string($regionsId) . "',
				ort = '" . mysql_real_escape_string(utf8_encode($ort)) . "',
				propId = '" . mysql_real_escape_string($propId) . "',
				icq = '" . mysql_real_escape_string($icq) . "',
				status = '" . mysql_real_escape_string($status) . "',
				varvadAvId = '" . mysql_real_escape_string($varvadAvId) . "',
				inloggad = '" . mysql_real_escape_string($inloggad) . "',
				inloggningar = '" . mysql_real_escape_string($inloggningar) . "',
				beskrivning = '" . mysql_real_escape_string(utf8_encode($beskrivning)) . "',
				avatar = '" . mysql_real_escape_string($imgName) . "'";
		mysql_query($sSql) or die(mysql_error());
		
		echo($sSql . "<br>\n");
	}
}

mysql_close();
?>